function Skills(){
    this.names = [
        {'id':1, 'mc':'炼狱', 'lev':3, 'lev1':3, 'lev2':6, 'lev3':9},
        {'id':2, 'mc':'刺客', 'lev':2, 'lev1':3, 'lev2':6, 'lev3':6},
        {'id':3, 'mc':'召唤使', 'lev':2, 'lev1':3, 'lev2':6, 'lev3':6},
        {'id':4, 'mc':'山脉', 'lev':1, 'lev1':2, 'lev2':2, 'lev3':2},
        {'id':5, 'mc':'魔法师', 'lev':2, 'lev1':3, 'lev2':6, 'lev3':6},
        {'id':6, 'mc':'极地', 'lev':3, 'lev1':2, 'lev2':4, 'lev3':6},
        {'id':7, 'mc':'掠食者', 'lev':1, 'lev1':3, 'lev2':3, 'lev3':3},
        {'id':8, 'mc':'森林', 'lev':1, 'lev1':3, 'lev2':3, 'lev3':3},
        {'id':9, 'mc':'德鲁伊', 'lev':1, 'lev1':2, 'lev2':2, 'lev3':2},
        {'id':10, 'mc':'雷霆', 'lev':3, 'lev1':2, 'lev2':3, 'lev3':4},
        {'id':11, 'mc':'守护神', 'lev':3, 'lev1':2, 'lev2':4, 'lev3':6},
        {'id':12, 'mc':'沙漠', 'lev':2, 'lev1':2, 'lev2':4, 'lev3':4},
        {'id':13, 'mc':'狂战士', 'lev':2, 'lev1':3, 'lev2':6, 'lev3':6},
        {'id':14, 'mc':'光', 'lev':3, 'lev1':3, 'lev2':6, 'lev3':9},
        {'id':15, 'mc':'游侠', 'lev':3, 'lev1':2, 'lev2':4, 'lev3':6},
        {'id':17, 'mc':'海洋', 'lev':3, 'lev1':2, 'lev2':4, 'lev3':6},
        {'id':18, 'mc':'月蚀', 'lev':1, 'lev1':2, 'lev2':2, 'lev3':2},
        {'id':19, 'mc':'剧毒', 'lev':1, 'lev1':3, 'lev2':3, 'lev3':3},
        {'id':20, 'mc':'云霄', 'lev':3, 'lev1':2, 'lev2':3, 'lev3':4},
        {'id':21, 'mc':'剑士', 'lev':3, 'lev1':2, 'lev2':4, 'lev3':6},
        {'id':22, 'mc':'影', 'lev':2, 'lev1':3, 'lev2':6, 'lev3':6},
        {'id':23, 'mc':'系魂', 'lev':1, 'lev1':2, 'lev2':2, 'lev3':2},
        {'id':24, 'mc':'钢铁', 'lev':3, 'lev1':2, 'lev2':3, 'lev3':4},
        {'id':25, 'mc':'水晶', 'lev':2, 'lev1':2, 'lev2':4, 'lev3':4},
        {'id':26, 'mc':'秘术', 'lev':2, 'lev1':2, 'lev2':4, 'lev3':4},
        {'id':27, 'mc':'炼金师', 'lev':1, 'lev1':1, 'lev2':1, 'lev3':1},
        {'id':28, 'mc':'大元素使', 'lev':1, 'lev1':1, 'lev2':1, 'lev3':1},
    ],
    this.mc = function(id){
        for(x in this.names){
            if(id==this.names[x].id){
                return this.names[x];
            }
        }
    },
    this.sk = function(id){
        // console.log(id);return;
        var arr = id.split(','), skmc = [];
        for(i=0; i<arr.length; i++){
            for(j in this.names){
                if(this.names[j].id==arr[i]){
                    skmc.push(this.names[j].mc);
                }
            }
        }
        return skmc.join('+');
    }
}
function Roles(){
    this.names = [{'id':1, 'pic':'戴安娜', 'skills':'1,2', 'gold':1, 'url':''},
    {'id':2, 'pic':'婕拉', 'skills':'1,3', 'gold':1, 'url':''},
    {'id':3, 'pic':'塔莉娅', 'skills':'4,5', 'gold':1, 'url':''},
    {'id':4, 'pic':'沃里克', 'skills':'6,7', 'gold':1, 'url':''},
    {'id':5, 'pic':'艾翁', 'skills':'8,9', 'gold':1, 'url':''},
    {'id':6, 'pic':'奥恩', 'skills':'10,11', 'gold':1, 'url':''},
    {'id':7, 'pic':'茂凯', 'skills':'8,9', 'gold':1, 'url':''},
    {'id':8, 'pic':'雷克顿', 'skills':'12,13', 'gold':1, 'url':''},
    {'id':9, 'pic':'薇恩', 'skills':'14,15', 'gold':1, 'url':''},
    {'id':10, 'pic':'内瑟斯', 'skills':'11,14', 'gold':1, 'url':''},
    {'id':11, 'pic':'弗拉基米尔', 'skills':'5,17', 'gold':1, 'url':''},
    {'id':12, 'pic':'雷欧娜', 'skills':'11,18', 'gold':1, 'url':''},
    {'id':13, 'pic':'克格莫', 'skills':'7,19', 'gold':1, 'url':''},
    {'id':14, 'pic':'沃利贝尔', 'skills':'6,10,13', 'gold':2, 'url':''},
    {'id':15, 'pic':'维鲁斯', 'skills':'1,15', 'gold':2, 'url':''},
    {'id':16, 'pic':'辛德拉', 'skills':'5,17', 'gold':2, 'url':''},
    {'id':17, 'pic':'亚索', 'skills':'20,21', 'gold':2, 'url':''},
    {'id':18, 'pic':'布隆', 'skills':'6,11', 'gold':2, 'url':''},
    {'id':19, 'pic':'雷克塞', 'skills':'7,24', 'gold':2, 'url':''},
    {'id':20, 'pic':'妮寇', 'skills':'8,9', 'gold':2, 'url':''},
    {'id':21, 'pic':'乐芙兰', 'skills':'2,5,8', 'gold':2, 'url':''},
    {'id':22, 'pic':'斯卡纳', 'skills':'7,25', 'gold':2, 'url':''},
    {'id':23, 'pic':'马尔扎哈', 'skills':'3,22', 'gold':2, 'url':''},
    {'id':24, 'pic':'诺提勒斯', 'skills':'11,17', 'gold':3, 'url':''},
    {'id':25, 'pic':'塞恩', 'skills':'13,22', 'gold':3, 'url':''},
    {'id':26, 'pic':'希维尔', 'skills':'12,21', 'gold':3, 'url':''},
    {'id':27, 'pic':'索拉卡', 'skills':'14,26', 'gold':3, 'url':''},
    {'id':28, 'pic':'千珏', 'skills':'1,15,22', 'gold':3, 'url':''},
    {'id':29, 'pic':'奇亚娜', 'skills':'1,2', 'gold':3, 'url':''},
    {'id':30, 'pic':'奇亚娜', 'skills':'2,4', 'gold':3, 'url':''},
    {'id':31, 'pic':'奇亚娜', 'skills':'2,17', 'gold':3, 'url':''},
    {'id':32, 'pic':'奇亚娜', 'skills':'2,20', 'gold':3, 'url':''},
    {'id':33, 'pic':'亚托克斯', 'skills':'14,21', 'gold':3, 'url':''},
    {'id':34, 'pic':'阿兹尔', 'skills':'3,12', 'gold':3, 'url':''},
    {'id':35, 'pic':'蒙多', 'skills':'13,19', 'gold':3, 'url':''},
    {'id':36, 'pic':'卡尔玛', 'skills':'18,26', 'gold':3, 'url':''},
    {'id':37, 'pic':'维迦', 'skills':'5,22', 'gold':3, 'url':''},
    {'id':38, 'pic':'魔腾', 'skills':'2,24', 'gold':3, 'url':''},
    {'id':39, 'pic':'伊泽瑞尔', 'skills':'6,15', 'gold':3, 'url':''},
    {'id':40, 'pic':'安妮', 'skills':'1,3', 'gold':4, 'url':''},
    {'id':41, 'pic':'卡兹克', 'skills':'2,12', 'gold':4, 'url':''},
    {'id':42, 'pic':'奥拉夫', 'skills':'6,13', 'gold':4, 'url':''},
    {'id':43, 'pic':'艾希', 'skills':'15,25', 'gold':4, 'url':''},
    {'id':44, 'pic':'卢锡安', 'skills':'14,23', 'gold':4, 'url':''},
    {'id':45, 'pic':'图奇', 'skills':'15,19', 'gold':4, 'url':''},
    {'id':46, 'pic':'迦娜', 'skills':'20,26', 'gold':4, 'url':''},
    {'id':47, 'pic':'莫菲特', 'skills':'4,11', 'gold':4, 'url':''},
    {'id':48, 'pic':'布兰德', 'skills':'1,5', 'gold':4, 'url':''},
    {'id':49, 'pic':'约里克', 'skills':'3,14', 'gold':4, 'url':''},
    {'id':50, 'pic':'易', 'skills':'21,22,26', 'gold':5, 'url':''},
    {'id':51, 'pic':'劫', 'skills':'2,3,10', 'gold':5, 'url':''},
    {'id':52, 'pic':'娜美', 'skills':'15,26', 'gold':5, 'url':''},
    {'id':53, 'pic':'辛吉德', 'skills':'19,27', 'gold':5, 'url':''},
    {'id':54, 'pic':'阿木木', 'skills':'1,11', 'gold':5, 'url':''},
    {'id':55, 'pic':'塔里克', 'skills':'11,25', 'gold':5, 'url':''},
    {'id':56, 'pic':'拉克丝', 'skills':'25,28', 'gold':7, 'url':''},
    {'id':57, 'pic':'拉克丝', 'skills':'10,28', 'gold':7, 'url':''},
    {'id':58, 'pic':'拉克丝', 'skills':'6,28', 'gold':7, 'url':''},
    {'id':59, 'pic':'拉克丝', 'skills':'1,28', 'gold':7, 'url':''},
    {'id':60, 'pic':'拉克丝', 'skills':'14,28', 'gold':7, 'url':''},
    {'id':61, 'pic':'拉克丝', 'skills':'24,28', 'gold':7, 'url':''},
    {'id':62, 'pic':'拉克丝', 'skills':'17,28', 'gold':7, 'url':''},
    {'id':63, 'pic':'拉克丝', 'skills':'22,28', 'gold':7, 'url':''},
    {'id':64, 'pic':'拉克丝', 'skills':'20,28', 'gold':7, 'url':''},
    {'id':65, 'pic':'拉克丝', 'skills':'8,28', 'gold':7, 'url':''},
    {'id':66, 'pic':'贾克斯', 'skills':'13,14', 'gold':2, 'url':''}],
    this.pic = function(id){
        for(x in this.names){
            if(this.names[x].id == id){
                return this.names[x].pic;
            }
        }
    }
}
function Judge(){
    this.roles = [],
    this.hasRole = function(name){
        return this.roles.indexOf(name)>-1? true: false;
    },
    this.skills = {},
    this.addSkill = function(id){
        var arr = id.split(',');
        for(i=0; i<arr.length;i++){
            var sk = skills.mc(arr[i]);
            if(this.skills['sk'+arr[i]] === undefined){
                var ar = {'id':arr[i], 'num':1, 'mc':sk.mc, 'lev':0};
                this.skills['sk'+arr[i]] = ar;
            }else{
                this.skills['sk'+arr[i]].num += 1;
            }
            this.lev(arr[i], sk);
        }
        this.view();
        // console.log(this.skills);
    },
    this.mvSkill = function(id){
        var arr = id.split(',');
        for(i=0; i<arr.length; i++){
            this.skills['sk'+arr[i]].num -= 1;
            if(this.skills['sk'+arr[i]].num < 1){
                delete this.skills['sk'+arr[i]];
            }else{
                var sk = skills.mc(arr[i]);
                this.lev(arr[i], sk);
            }
        }
        this.view();
        // console.log(this.skills);
    },
    this.lev = function(i, sk){
        switch(sk.lev){
            case 1:
                this.skills['sk'+i].lev = this.skills['sk'+i].num>=sk.lev1? 3: 0;
                break;
            case 2:
                if(this.skills['sk'+i].num>=sk.lev2){
                    this.skills['sk'+i].lev = 3;
                }else if(this.skills['sk'+i].num>=sk.lev1){
                    this.skills['sk'+i].lev = 1;
                }else{
                    this.skills['sk'+i].lev = 0;
                }
                break;
            case 3:
                if(this.skills['sk'+i].num>=sk.lev3){
                    this.skills['sk'+i].lev = 3;
                }else if(this.skills['sk'+i].num>=sk.lev2){
                    this.skills['sk'+i].lev = 2;
                }else if(this.skills['sk'+i].num>=sk.lev1){
                    this.skills['sk'+i].lev = 1;
                }else{
                    this.skills['sk'+i].lev = 0;
                }
                break;
            default:
                break;
        }
    },
    this.view = function(){
        jb.innerHTML = '';
        for(x in this.skills){
            // console.log(this.skills[x]);
            if(this.skills[x].lev>0){
                var cs = this.skills[x].lev==3? 'highlev': 'lowlev';
                var btn = document.createElement('button');
                btn.setAttribute('class', cs);
                btn.innerHTML = this.skills[x].num+'  '+this.skills[x].mc;
                jb.appendChild(btn);
            }
        }
        rks.innerHTML = this.roles.length;
    }
}
var skills = new Skills(), roles = new Roles(), judge = new Judge();
var win = document.getElementById('win'), 
goldOne = document.getElementById('goldOne'),
goldTwo = document.getElementById('goldTwo'), 
goldThree = document.getElementById('goldThree'), 
goldFour = document.getElementById('goldFour'), 
goldFive = document.getElementById('goldFive'), 
jb = document.getElementById('jb'),
rks = document.getElementById('rks'),
goldSeven = document.getElementById('goldSeven');
for(x in roles.names){
    var btn = document.createElement('button');
    btn.setAttribute('onclick', 'tj('+roles.names[x].id+',"'+roles.names[x].skills+'")');
    btn.setAttribute('title', ''+skills.sk(roles.names[x].skills));
    btn.innerHTML = roles.names[x].pic;
    if(1==roles.names[x].gold){
        goldOne.appendChild(btn);
    }else if(2==roles.names[x].gold){
        goldTwo.appendChild(btn);
    }else if(3==roles.names[x].gold){
        goldThree.appendChild(btn);
    }else if(4==roles.names[x].gold){
        goldFour.appendChild(btn);
    }else if(5==roles.names[x].gold){
        goldFive.appendChild(btn);
    }else if(7==roles.names[x].gold){
        goldSeven.appendChild(btn);
    }
}


//移除角色
function yc(obj, skill, id){
    judge.roles.splice(judge.roles.indexOf(id), 1);
    win.removeChild(obj);
    judge.mvSkill(skill);
    
    // console.log(skill);
}

//添加角色
function tj(id, skill){
    // console.log(id);
    var name = roles.pic(id);
    if(!judge.hasRole(id)){
        if(judge.roles.length>8){
            alert('最大人口数为9');
            return;
        }
        judge.roles.push(id);
        judge.addSkill(skill);
        var btn = document.createElement('button');
        btn.setAttribute('onclick', 'yc(this,"'+skill+'",'+id+')');
        btn.innerHTML = name;
        win.appendChild(btn);
    }

    // console.log(judge.roles);
    // console.log(judge.skills);
}